package com.yq.day72;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
 * @author 3307235355@qq.com
 * @description
 * @since 2022/07/02 11:41
 * 创建一个学会对象，不使用new的方式，使用反射和注解实现
 * name的长度在5之间 ，年龄在18-25之间
 */

//测试类
public class Demo04 {
    //定义一个字节码的文件对象，进行赋值
    static  Class<?> aClass;
    static {
        try {
            aClass = Class.forName("com.yq.day72.Student");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        Student zhansa = getInstance("zga", 293);
        System.out.println(zhansa);

    }

    public static Student getInstance(String name,int age) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
        //名字的校验  获取student的属性name
        Field name1 = aClass.getDeclaredField("name");
        //判断是否使用注解 判断属性name上是否有注解
        boolean a1 = name1.isAnnotationPresent(NameLimit.class);
        if (a1){
            //获取注解的实例
            NameLimit a2 = name1.getAnnotation(NameLimit.class);
            //获取注解的属性值
            int length = a2.length();
            //判断
            if (name.length()>length){
                throw  new IllegalArgumentException("长度大");
            }
        }

        //对年龄的校验 获取age的属性
        Field age1 = aClass.getDeclaredField("age");
        //判断是否有注解
        boolean annotationPresent = age1.isAnnotationPresent(AgeLimit.class);
        if (annotationPresent){
            //获取注解的实例
            AgeLimit annotation = age1.getAnnotation(AgeLimit.class);
            //获取属性值
            int i = annotation.maxAge();
            int i1 = annotation.minAge();
            if (age>i || age <i1){
                throw new IllegalArgumentException("年龄不合法！！！！！");
            }
        }

        //获取构造方法
        Constructor<?> declaredConstructor = aClass.getDeclaredConstructor(String.class, int.class);
        //忽略限制
        declaredConstructor.setAccessible(true);
        //创建学生对象
        Student o = (Student) declaredConstructor.newInstance(name, age);
        return o;
    }
}

class Student{
    //注解
    @NameLimit
    String name;
    //注解
    @AgeLimit
    int age;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    private Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
}


//元注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface NameLimit{
    int length() default 5;
}

//元注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface AgeLimit{
    int maxAge() default 25;
    int minAge() default 18;
}